﻿#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
using NUnit.Framework;

namespace Drill.Configuration
{
	[TestFixture]
	public class XmlConfigurationReaderTests
	{
		[Test]
		public void CanConstructFromFile()
		{
			// Arrange
			string filename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

			// Act
			XmlConfigurationReader reader = XmlConfigurationReader.FromFile(filename);

			// Arrange
			Assert.That(reader, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "filename", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructFromFile()
		{
			// Arrange
			string filename = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			XmlConfigurationReader.FromFile(filename);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanConstructFromManifestResource()
		{
			// Arrange
			const string manifestResourceName = "Drill.TestConfigAsManifestResource.xml";

			// Act
			XmlConfigurationReader reader = XmlConfigurationReader.FromManifestResource(manifestResourceName);

			// Arrange
			Assert.That(reader, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "manifestResourceName", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructFromManifestResource()
		{
			// Arrange
			string manifestResourceName = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			XmlConfigurationReader.FromManifestResource(manifestResourceName);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanConstructFromStream()
		{
			// Arrange
			string filename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
			using(Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
			{
				// Act
				XmlConfigurationReader reader = XmlConfigurationReader.FromStream(stream);

				// Arrange
				Assert.That(reader, Is.Not.Null);
			}
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "stream", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructFromStream()
		{
			// Arrange
			Stream stream = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			XmlConfigurationReader.FromStream(stream);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanConstructFromXDocument()
		{
			// Arrange
			string filename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
			XDocument xdocument = XDocument.Load(filename);

			// Act
			XmlConfigurationReader reader = XmlConfigurationReader.FromXDocument(xdocument);

			// Arrange
			Assert.That(reader, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "xdocument", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructFromXDocumentWithNullXDocumentArgument()
		{
			// Arrange
			XDocument xdocument = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			XmlConfigurationReader.FromXDocument(xdocument);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanConstructFromXmlString()
		{
			// Arrange
			const string xml = "<dependencyResolver name=\"Default\"><drillBits><drillBit name=\"Default\" type=\"Drill.DrillBits.Unity.UnityDrillBit, Drill.DrillBits.Unity\"><cfgSources><cfgSource key=\"UnityConfig\" method=\"ConfigFile\" /></cfgSources></drillBit></drillBits></dependencyResolver>";

			// Act
			XmlConfigurationReader reader = XmlConfigurationReader.FromXmlString(xml);

			// Arrange
			Assert.That(reader, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "xml", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructFromXmlStringWithNullXmlArgument()
		{
			// Arrange
			string xml = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			XmlConfigurationReader.FromXmlString(xml);
			// ReSharper restore ExpressionIsAlwaysNull

			// Arrange
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}


		[Test]
		public void CanReadAllDependencyResolversConfiguration()
		{
			// Arrange
			string filename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
			XmlConfigurationReader reader = XmlConfigurationReader.FromFile(filename);

			// Act
			var allConfigs = reader.ReadAllDependencyResolversConfiguration();

			// Assert
			Assert.That(allConfigs, Is.Not.Null);
			Assert.That(allConfigs.Count, Is.EqualTo(1));
		}

		[Test]
		public void CanReadDependencyResolverConfigurationWithAppConfig()
		{
			// Arrange
			const string name = "Default";
			string filename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
			XmlConfigurationReader reader = XmlConfigurationReader.FromFile(filename);

			// Act
			var config = reader.ReadDependencyResolverConfiguration(name);

			// Assert
			Assert.That(config, Is.Not.Null);

			IDependencyResolver dependencyResolver = DependencyResolverManager.Create(config);

			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanReadDependencyResolverConfigurationWithTestAlternateAppConfig()
		{
			// Arrange
			const string name = "Default";
			string configDir = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
			Debug.Assert(configDir != null, "configDir != null");
			string filename = Path.Combine(configDir, "TestAlternateAppConfig.xml");
			XmlConfigurationReader reader = XmlConfigurationReader.FromFile(filename);

			// Act
			var config = reader.ReadDependencyResolverConfiguration(name);

			// Assert
			Assert.That(config, Is.Not.Null);

			IDependencyResolver dependencyResolver = DependencyResolverManager.Create(config);

			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanReadDependencyResolverConfigurationWithTestConfig1()
		{
			// Arrange
			const string name = "Default";
			string configDir = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
			Debug.Assert(configDir != null, "configDir != null");
			string filename = Path.Combine(configDir, "TestConfig1.xml");
			XmlConfigurationReader reader = XmlConfigurationReader.FromFile(filename);

			// Act
			var config = reader.ReadDependencyResolverConfiguration(name);

			// Assert
			Assert.That(config, Is.Not.Null);

			IDependencyResolver dependencyResolver = DependencyResolverManager.Create(config);

			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanReadDependencyResolverConfigurationWithTestConfig2()
		{
			// Arrange
			const string name = "Default";
			string configDir = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
			Debug.Assert(configDir != null, "configDir != null");
			string filename = Path.Combine(configDir, "TestConfig2.xml");
			XmlConfigurationReader reader = XmlConfigurationReader.FromFile(filename);

			// Act
			var config = reader.ReadDependencyResolverConfiguration(name);

			// Assert
			Assert.That(config, Is.Not.Null);

			IDependencyResolver dependencyResolver = DependencyResolverManager.Create(config);

			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanReadDependencyResolverConfigurationWithTestConfig3()
		{
			// Arrange
			const string name = "Default";
			string configDir = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
			Debug.Assert(configDir != null, "configDir != null");
			string filename = Path.Combine(configDir, "TestConfig3.xml");
			XmlConfigurationReader reader = XmlConfigurationReader.FromFile(filename);

			// Act
			var config = reader.ReadDependencyResolverConfiguration(name);

			// Assert
			Assert.That(config, Is.Not.Null);

			IDependencyResolver dependencyResolver = DependencyResolverManager.Create(config);

			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanReadDependencyResolverConfigurationWithTestConfig4()
		{
			// Arrange
			const string name = "Default";
			string configDir = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
			Debug.Assert(configDir != null, "configDir != null");
			string filename = Path.Combine(configDir, "TestConfig4.xml");
			XmlConfigurationReader reader = XmlConfigurationReader.FromFile(filename);

			// Act
			var config = reader.ReadDependencyResolverConfiguration(name);

			// Assert
			Assert.That(config, Is.Not.Null);

			IDependencyResolver dependencyResolver = DependencyResolverManager.Create(config);

			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanReadDependencyResolverConfigurationWithTestConfigAsManifestResource()
		{
			// Arrange
			const string name = "Default";
			const string manifestResourceName = "Drill.TestConfigAsManifestResource.xml";
			XmlConfigurationReader reader = XmlConfigurationReader.FromManifestResource(manifestResourceName);

			// Act
			var config = reader.ReadDependencyResolverConfiguration(name);

			// Assert
			Assert.That(config, Is.Not.Null);

			IDependencyResolver dependencyResolver = DependencyResolverManager.Create(config);

			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanReadDependencyResolverConfigurationWithTestConfigAsManifestResourceWithAssemblyArgument()
		{
			// Arrange
			const string name = "Default";
			const string manifestResourceName = "Drill.TestConfigAsManifestResource.xml";
			Assembly assembly = Assembly.GetExecutingAssembly();
			XmlConfigurationReader reader = XmlConfigurationReader.FromManifestResource(manifestResourceName, assembly);

			// Act
			var config = reader.ReadDependencyResolverConfiguration(name);

			// Assert
			Assert.That(config, Is.Not.Null);

			IDependencyResolver dependencyResolver = DependencyResolverManager.Create(config);

			Assert.That(dependencyResolver, Is.Not.Null);
		}
	}
}